package com.dp.service.impl3;


import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.StringTokenizer;
import java.util.Vector;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import com.dp.pub.MyServer;
import com.dp.util.BaseBean;
import com.dp.util.DataMap;
import com.dp.util.KeyGenerator;
import com.dp.util.MD5;
import com.dp.util.PageBean;

@Service
@Transactional
public class UserBeanImpl extends BaseImpl {
	@Autowired private ValidBeanImpl validBean;
    public boolean RoleManageEdit(DataMap hm) {
     
        ArrayList arrayListSql = new ArrayList();
        ArrayList t_user_auth_sql = new ArrayList();
        DataMap hashMap = null;
        ArrayList arrayList = null;
        boolean isTrue = false;
        //String role_auth_id = null;
       // String user_auth_id = null;
        String sql = null;

        String nowTime =MyServer.sdf.format(new Date());// super.getNowTime("yyyy-MM-dd HH:mm:ss");
        String str = hm.get("gvalue").toString();
        String split = "|";
        StringTokenizer token = new StringTokenizer(str, split);
        String[] sqlBatch;

        //删除 角色(岗位)配置字典表 特定数据
       

        int i = 0;

        try {
            arrayList = this.queryALLResultSet("select user_id,employee_id,role_id,ctrl_date,user_pwd,auth_tupe,can_access,code" +
            		" from t_user where role_id='" + hm.get("role_id") + "'");

            //删除 用户权限表 特定数据 
            //删除这样角色的用户的原来的从角色继承的权限
            sql = "delete from t_user_auth where user_id in(";
            i = 0;

            for (i = 0; i < arrayList.size(); i++) {
                hashMap = (DataMap) arrayList.get(i);
                sql = sql + "'" + hashMap.get("user_id") + "',";
            }

            sql = sql + "'safsafsafsafsa')";
            
            sql+=" and submenu_id in (select submenu_id from t_role_menu where role_id='" +   hm.get("role_id") + "')";
            
            arrayListSql.add(sql);
            arrayListSql.add("delete from t_role_menu where role_id='" + hm.get("role_id") + "'");
            i = 1;

            List<String> needadd=new ArrayList<String>();//需要增加的权限
            String temp_submenu="";
            
            //添加 角色(岗位)配置字典表 特定数据
            while (token.hasMoreTokens()) {
                //role_auth_id = KeyGenerator.getInstance().getNextKeyValue("t_role_auth");
            	temp_submenu=token.nextToken();
                //user_auth_id = KeyGenerator.getInstance().getNextKeyValue("t_user_auth");
                sql = "insert into t_role_menu (role_id,submenu_id) values('" +
                   hm.get("role_id") + "','" +temp_submenu + "')";

                // t_user_auth_sql.add("insert into t_user_auth (user_auth_id,employee_id,node2_id,ctrl_date) values('" + user_auth_id + "','" + hashMap.get("employee_id")+ "','" + token.nextToken() + "','" + nowTime + "')");                
                arrayListSql.add(sql);
                needadd.add(temp_submenu);

                //i++;
            }

            //添加 用户权限表 特定数据
            i = 1;
            //user_auth_id = KeyGenerator.getInstance().getNextKeyValue("t_user_auth");

           

            //组合sql        
            for (i = 0; i < t_user_auth_sql.size(); i++) {
                arrayListSql.add(t_user_auth_sql.get(i));
            }

            sqlBatch = new String[arrayListSql.size()];

            for (i = 0; i < arrayListSql.size(); i++) {
                sqlBatch[i] = this.ObjectToString(arrayListSql.get(i));
                

                //System.out.println(sqlBatch[i]);
            }

            isTrue = this.execTransaction(sqlBatch);
            
            //首先查出用户已经具有的，而且是本次角色已经增加的，然后从新增中去除，在加入用户权限表，防止重复
            sql = "select user_id,submenu_id from t_user_auth where user_id in(";
            for (i = 0; i < arrayList.size(); i++) {
                hashMap = (DataMap) arrayList.get(i);
                sql = sql + "'" + hashMap.get("user_id") + "',";
            }

            sql = sql + "'safsafsafsafsa')";            
            sql+=" and submenu_id in ( ";
            for(String s:needadd)
            {
            	sql+="'"+s+"',";
            }
            sql+="'ss')";
            System.out.println(sql);
           List<DataMap> has= this.queryALLResultSet(sql);
           
          
           t_user_auth_sql.clear();
            String user_id="";
            String submenu="";
            boolean need=true;
            for (int i1 = 0; i1 < arrayList.size(); i1++) {
                user_id = ((DataMap) arrayList.get(i1)).getMapData("user_id");                
                for(String s:needadd)
                {
                	need=true;
                    for(DataMap da:has)
                    {
                    	if(user_id.equals(da.getMapData("user_id"))&& s.equals(da.getMapData("submenu_id")))
                    	{
                    		need=false;
                    		break;  
                    	}
                    }
                    if(need) t_user_auth_sql.add(
                    		"insert into t_user_auth (user_id,submenu_id) values('" + user_id+ "','" + s +"')");
                }
                //token = new StringTokenizer(str, split);

           }
            
            sqlBatch = new String[t_user_auth_sql.size()];
            for (i = 0; i < t_user_auth_sql.size(); i++) {
                sqlBatch[i] = this.ObjectToString(t_user_auth_sql.get(i));
            }
            this.execTransaction(sqlBatch);
            
        } catch (Exception e) {
            e.printStackTrace();
        }

        return isTrue;
    }

    public boolean DeleteAllRole(DataMap hashMap) {
       // String value_id = KeyGenerator.getInstance().getNextKeyValue("t_managedept");
    	ArrayList arrayList=null;
    	try {
			 arrayList = this.queryALLResultSet(
			         "select * from t_user where role_id='" +
			         hashMap.get("role_id") + "'");
		} catch (Exception e1) 
		{
			e1.printStackTrace();
			return false;
		}
		String sql = "delete from t_user_auth where user_id in(";
        

        for (int i = 0; i < arrayList.size(); i++) {
            hashMap = (DataMap) arrayList.get(i);
            sql = sql + "'" + hashMap.get("user_id") + "',";
        }
        sql = sql + "'safsafsafsafsa')";
        String[] sqls = new String[3];
        sqls[0]=sql;
        
        
        sqls[1] = "delete from t_role_menu where role_id =" +
            super.ObjectToStringDB(hashMap.get("role_id"));
        sqls[2] = "delete from t_roles where role_id =" +
            super.ObjectToStringDB(hashMap.get("role_id"));

        //hashMap.put
        //System.out.println(sql[0]);
        //System.out.println(sql[1]);
        try {
            return this.execTransaction(sqls);
        } catch (Exception e) {
            return false;
        }
    }

    public ArrayList getRolename1List(DataMap hm) {
        ArrayList aNode1 = new ArrayList();
//        String sql =
//            "select n2.node1_id from t_rolenode2 ra,t_node2 n2 where n2.is_issue='Y' and ra.node2_id=n2.node2_id and ra.rolenode1_id='" +
//            hm.get("rolenode1_id") + "'";

        String sql =
            "select n2.mainmenu_id from t_role_menu ra,t_submenus n2 where n2.is_use='Y' and ra.submenu_id=n2.submenu_id and ra.role_id='" +
            hm.get("role_id") + "'";
        //String sql = "select n2.node1_id from t_user_auth ra,t_node2 n2 where ra.node2_id=n2.node2_id and ra.employee_id='" + hm.get("t_employee").toString() + "'";
      
        try {
            aNode1 = this.queryALLResultSet(sql);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return aNode1;
    }

    public ArrayList getRolename2List(DataMap hm) {
        ArrayList aNode2 = new ArrayList();
        String sql = "select submenu_id from t_role_menu where role_id='" +
            hm.get("role_id") + "'";

        //String sql = "select node2_id from t_user_auth where employee_id='" + hm.get("t_employee").toString() + "'";
       

        try {
            aNode2 = this.queryALLResultSet(sql);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return aNode2;
    }

    public boolean Addrolename(DataMap hashMap) {
    	 sql ="select count(*) as cout from t_roles where role_name="+ super.ObjectToStringDB(hashMap.get("role_name"));
    	//System.out.println(sql);
    
    	try {
    		if(this.getOneInt()>0)return false;
			//if(!"0".equals(db.queryOne(sql)))
				
//			/	return false;
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			return false;
		}
    	
        String role_id = KeyGenerator.getInstance().getNextKeyValue("t_roles");
         sql = "insert into t_roles(role_id,role_name,role_type) VALUES(";
        sql = sql + "'" + role_id + "',";
        sql = sql + super.ObjectToStringDB(hashMap.get("role_name"))+ ",";
        sql = sql + super.ObjectToStringDB(hashMap.get("role_type")) + ")";
        
        System.out.println(sql);
        hashMap.put("role_id", role_id);

        try {
        	this.updateNativeQuery(sql);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

//    public ArrayList getRole() {
//        String sql = "select DISTINCT * from t_roles ";
//
//        try {
//            return this.queryALLResultSet(sql);
//        } catch (Exception e) {
//            return null;
//        }
//    }
//    public ArrayList getCompany() {
//        String sql = "select DISTINCT * from t_spcode";
//
//        try {
//            return this.queryALLResultSet(sql);
//        } catch (Exception e) {
//            return null;
//        }
//    }

//    public PageBean RoleManage(DataMap hashMap) {
//        String sql = "select DISTINCT * from t_roles ";
//        Object role_name = hashMap.get("role_name");
//
//        if (this.StringIsNull(role_name)) {
//            sql = sql + " where role_name like '%" + role_name + "%'";
//        }
//
//        sql = sql + " order by role_id  ";
//
//      // System.out.println(sql);
//        try {
//            hashMap.put("sql", sql);
//
//            return new PageBean(hashMap);
//        } catch (Exception e) {
//            return null;
//        }
//    }

//    public  ArrayList getUser() {
//        try {
//            return this.queryALLResultSet(
//                "select * from t_user order by ctrl_date ");
//        } catch (Exception e) {
//            e.printStackTrace();
//
//            return null;
//        }
//    }
//    public  ArrayList getUser(String employee_superior) {
//        try {
//            return this.queryALLResultSet(
//                "select * from t_employee  where employee_superior='"+employee_superior+"' order by employee_id ");
//        } catch (Exception e) {
//            e.printStackTrace();
//
//            return null;
//        }
//    }

//    public DataMap getUser(Object employee_id) {
//        String sql =
//            "SELECT t1.*,t2.*,t3.*,t4.*,t5.employee_name as employee_superior_txt,t6.user_id,t6.user_id,t6.user_pwd" +
//            " FROM t_employee as t1 " +
//            "Left  Join t_rolenode1 as t2 on t1.rolenode1_id=t2.rolenode1_id " +
//            "Left  Join t_group     as t3 on t1.group_id=t3.group_id " +
//            "Left  Join t_section   as t4 on t3.section_id=t4.section_id  or t1.section_id=t4.section_id " +
//            "Left  Join t_employee  as t5 on t5.employee_superior=t1.employee_id " +
//            "Left  Join t_user   as t6 on t6.employee_id=t1.employee_id " +
//            "where 1=1 	";
//
//        if (this.StringIsNull(employee_id)) {
//            sql = sql + "  and t1.employee_id='" + employee_id + "'";
//        }
//
//        sql = sql + " order by t1.employee_name  ";
//        System.out.println(sql);
//
//        try {
//            //return (DataMap) this.queryOneRow(sql);
//        } catch (Exception e) {
//            return null;
//        }
//        return null;
//    }
    public DataMap getUser(Object employee_id) {
        
//    	"SELECT * FROM t_user as t1 , t_employee as t2 " +
//    	"Left  Join t_roles as t3 on t1.role_id=t3.role_id " +
//    	"WHERE t1.employee_id = t2.employee_id ";
    	String sql ="SELECT employee_id,sp_code,employee_name,employee_phone,employee_email,group_leader,job,info FROM t_employee as t1 " +
    	"Left  Join t_company as t4 on t1.company_id=t4.company_id " +
    	", t_user as t2 " +
    	"Left  Join t_roles as t3 on t2.role_id=t3.role_id " +
    	
    	" where t1.employee_id=t2.employee_id and t1.employee_id='"+employee_id+"'";
    	
    	
    	
    	sql="select user_id,spcode,a.role_id,d.group_leader as isgroup,can_access from t_user a,t_spcode b,t_roles c,t_employee d where a.employee_id=d.employee_id and d.sp_code=b.spcode  and a.role_id=c.role_id " +
    			" and a.user_id='"+employee_id+"'";
    	

   // sql = sql + " order by t1.ctrl_date desc  ";
	
	    System.out.println(sql);
	
	    try {
	    	return null;// (DataMap) this.queryOneRow(sql);
	    } catch (Exception e) {
	        return null;
	    }
}

//    public PageBean getUser(DataMap hashMap) {
//        
////        	"SELECT * FROM t_user as t1 , t_employee as t2 " +
////        	"Left  Join t_roles as t3 on t1.role_id=t3.role_id " +
////        	"WHERE t1.employee_id = t2.employee_id ";
//        	String sql ="SELECT * FROM t_employee as t1 " +
//        	"Left  Join t_company as t4 on t1.company_id=t4.company_id " +
//        	", t_user as t2 " +
//        	"Left  Join t_roles as t3 on t2.role_id=t3.role_id " +
//        	
//        	" where t1.employee_id=t2.employee_id ";
//        	//20090611 add
//        	//sql="select user_id,spcode,spname,role_name from t_user a,t_spcode b,t_roles c where a.employee_id=b.spcode and a.role_id=c.role_id";
//        	sql="select user_id,spcode,spname,role_name,can_access from t_user a,t_spcode b,t_roles c,t_employee d where a.employee_id=d.employee_id and d.sp_code=b.spcode and a.role_id=c.role_id ";
//        	
//        Object user_id = hashMap.get("user_id");
//
//        Object spcode = hashMap.get("spcode");
//
//        if (this.StringIsNull(spcode)) {
//            sql = sql + "  and a.employee_id like '%" + spcode + "%'";
//        }
//        if (this.StringIsNull(user_id)) {
//            sql = sql + "  and a.user_id like '%" + user_id + "%'";
//        }
//
//       
//
//       // sql = sql + " order by t1.ctrl_date desc  ";
//
//        System.out.println(sql);
//
//        try {
//            hashMap.put("sql", sql);
//
//            return new PageBean(hashMap);
//        } catch (Exception e) {
//            return null;
//        }
//    }

    public  ArrayList getArrayList(String sql) {
        try {
            return this.queryALLResultSet(sql);
        } catch (Exception e) {
            return new ArrayList();
        }
    }

//    public boolean AddUser(DataMap hashMap) {
//        //String value_id=KeyGenerator.getInstance().getNextKeyValue("t_employee");	
//    	
//    	if(hashMap.get("company_id")==null) return false;    
//        Object role_id = hashMap.get("role_id");
//
//        //System.out.println(hashMap);
//        ArrayList arrayList = null;
//        DataMap hashMapDb = null;
//        String nowTime = null;
//        String employee_id = KeyGenerator.getInstance().getNextKeyValue("t_employee");
//        
//        String id = null; // KeyGenerator.getInstance().getNextKeyValue("t_role_auth");
//
//        System.out.println("******************	" + employee_id);
//
//        if (super.StringIsNull(role_id)) {
//                     
//            arrayList = this.getArrayList( "select * from t_role_menu where role_id='" +role_id + "'");
//        } else {
//            arrayList = new ArrayList();
//
//            // String sql = "delete from t_user_auth where employee_id='" + hm.get("t_employee").toString() + "'";           
//        }
//
//        ArrayList sqlList = new ArrayList();
//        String sql=null;
//       /**/ sql= "insert into t_employee(employee_id,employee_name,employee_phone,employee_email,group_leader,sp_code)" +
//            "VALUES( ";
//        sql = sql + super.ObjectToStringDB(employee_id) + ", ";
//        sql = sql + super.ObjectToStringDB(hashMap.get("employee_name")) +",";        
//        sql = sql + super.ObjectToStringDB(hashMap.get("employee_phone")) +",";
//        sql = sql + super.ObjectToStringDB(hashMap.get("employee_email")) +",";
//        sql = sql + super.ObjectToStringDB(hashMap.get("group_leader")) +",";
//        sql = sql + super.ObjectToStringDB(hashMap.get("company_id")) +")";
//       
//        sqlList.add(sql);
//        System.out.println(sql);//isgroup
//        
//        sql = "insert into t_user(user_id,employee_id,role_id,can_access,ctrl_date,user_pwd,auth_tupe)" +
//        "VALUES( ";
//        sql = sql + super.ObjectToStringDB(hashMap.get("user_id")) +",";
//        sql = sql + super.ObjectToStringDB(employee_id) + ", ";
//        sql = sql + super.ObjectToStringDB(hashMap.get("role_id")) +",";
//        sql = sql + super.ObjectToStringDB(hashMap.get("can_access")) +",";
//        sql = sql + super.ObjectToStringDB(super.getNowTime()) +",";
//        sql = sql + super.ObjectToStringDB(MD5.StringMD5(hashMap.getMapData("user_pwd"))) +",";
//        //sql = sql + super.ObjectToStringDB(hashMap.get("auth_tupe")) +")";
//        sql = sql + "'WR'" +")";
//        sqlList.add(sql);
//        System.out.println(sql);
//
//        for (int i = 0; i < arrayList.size(); i++) {
//            hashMapDb = (DataMap) arrayList.get(i);
//            id = "role_auth_id" + "_" + i;
//            sql = "insert into t_user_auth (submenu_id,user_id) values( " ;
//            sql = sql + super.ObjectToStringDB(hashMapDb.get("submenu_id")) +",";
//            sql = sql + super.ObjectToStringDB(hashMap.get("user_id")) +")";
//                
//
//            //System.out.println(sql[i + 1]);
//            sqlList.add(sql);
//        }
//
//        try {
//            return this.execTransaction(sqlList);
//        } catch (Exception e) {
//            e.printStackTrace();
//
//            return false;
//        }
//    }
    public boolean ModifyUser(DataMap hashMap) {
        //String value_id=KeyGenerator.getInstance().getNextKeyValue("t_employee");	
        Object role_id = hashMap.get("role_id");

        //System.out.println(hashMap);
        ArrayList arrayList = null;
        DataMap hashMapDb = null;
        String nowTime = null;
        String employee_id = hashMap.getMapData("company_id");
        
        String id = null; // KeyGenerator.getInstance().getNextKeyValue("t_role_auth");

        

        //if (super.StringIsNull(role_id)) {
                    
           // arrayList = this.getArrayList( "select * from t_role_menu where role_id='" +role_id + "'");
        //} else {
          //  arrayList = new ArrayList();

            // String sql = "delete from t_user_auth where employee_id='" + hm.get("t_employee").toString() + "'";           
       // }

        ArrayList sqlList = new ArrayList();
        
        String sql=null;
        //sql="delete from t_employee where employee_id="+super.ObjectToStringDB(employee_id);
       // sqlList.add(sql);
       // sql="delete from t_user where user_id="+super.ObjectToStringDB(hashMap.get("user_id"));
       // sqlList.add(sql);
       // sql="delete from t_user_auth where user_id="+super.ObjectToStringDB(hashMap.get("user_id"));
       // sqlList.add(sql);
        
       /* employee_id = KeyGenerator.getInstance().getNextKeyValue("t_employee");
        sql= "insert into t_employee(employee_id,employee_name,employee_tel,employee_phone,employee_email,group_leader,company_id,group_id)" +
            "VALUES( ";
        sql = sql + super.ObjectToStringDB(employee_id) + ", ";
        sql = sql + super.ObjectToStringDB(hashMap.get("employee_name")) +",";
        sql = sql + super.ObjectToStringDB(hashMap.get("employee_tel")) +",";
        sql = sql + super.ObjectToStringDB(hashMap.get("employee_phone")) +",";
        sql = sql + super.ObjectToStringDB(hashMap.get("employee_email")) +",";
        sql = sql + super.ObjectToStringDB(hashMap.get("group_leader")) +",";
        sql = sql + super.ObjectToStringDB(hashMap.get("company_id")) +",";
        sql = sql + super.ObjectToStringDB(hashMap.get("group_id")) +")";
       
        sqlList.add(sql);
        System.out.println(sql);*/
        
       /* sql = "insert into t_user(user_id,employee_id,role_id,ctrl_date,isgroup,user_pwd,auth_tupe)" +
        "VALUES( ";
        sql = sql + super.ObjectToStringDB(hashMap.get("user_id")) +",";
        sql = sql + super.ObjectToStringDB(employee_id) + ", ";
        sql = sql + super.ObjectToStringDB(hashMap.get("role_id")) +",";
        sql = sql + super.ObjectToStringDB(super.getNowTime()) +",";
        sql = sql + super.ObjectToStringDB(hashMap.get("group_leader")) +",";
        sql = sql + super.ObjectToStringDB(MD5.StringMD5(hashMap.getMapData("user_pwd"))) +",";
        //sql = sql + super.ObjectToStringDB(hashMap.get("auth_tupe")) +")";
        sql = sql + "'WR'" +")";
        */
        String s=hashMap.getMapData("user_pwddd");
        
        
        sql="update t_user set can_access="+hashMap.get("can_access")+", role_id="+super.ObjectToStringDB(hashMap.get("role_id"))
        +((s==null||"".equals(s))?"":(" ,user_pwd=" +super.ObjectToStringDB(MD5.StringMD5(hashMap.getMapData("user_pwd"))))) +" where user_id="+super.ObjectToStringDB(hashMap.get("user_id"));
             
        sqlList.add(sql);
        
        sql="update t_employee set sp_code='"+employee_id+"' ,group_leader="+super.ObjectToStringDB(hashMap.get("group_leader")) +" where employee_id=" +
        "(select employee_id from t_user where user_id="+ super.ObjectToStringDB(hashMap.get("user_id"))+")";     
       sqlList.add(sql);
        
     /*
        for (int i = 0; i < arrayList.size(); i++) {
            hashMapDb = (DataMap) arrayList.get(i);
            id = "role_auth_id" + "_" + i;
            sql = "insert into t_user_auth (submenu_id,user_id) values( " ;
            sql = sql + super.ObjectToStringDB(hashMapDb.get("submenu_id")) +",";
            sql = sql + super.ObjectToStringDB(hashMap.get("user_id")) +")";
                

            //System.out.println(sql[i + 1]);
            sqlList.add(sql);
        }*/

        try {
            return this.execTransaction(sqlList);
        } catch (Exception e) {
            e.printStackTrace();

            return false;
        }
    }

    public boolean UpdateUser(DataMap hashMap) {
        String sql = "insert into t_user (user_id,employee_id,ctrl_date,user_pwd)values(";
        sql = sql + super.ObjectToStringDB(hashMap.get("user_id")) + ",";
        sql = sql + super.ObjectToStringDB(hashMap.get("employee_id")) + ",";
        sql = sql + super.ObjectToStringDB(super.getNowTime()) + ",";
        sql = sql + super.ObjectToStringDB(hashMap.get("user_id")) + ")";
        System.out.println(sql);

        try {
            return this.execUpdate(sql);
        } catch (Exception e) {
            return false;
        }
    }

    public boolean Repassword(DataMap hashMap) {
        if (!validBean.validLogin(hashMap.getMapData("user_id"),hashMap.getMapData("ypwd"))) {
            return false;
        }

        String sql = "update t_user set";
        sql = sql + " user_pwd=" +
            this.ObjectToStringDB(MD5.StringMD5(hashMap.getMapData("repwd1")));
        sql = sql + " where user_id='" + hashMap.get("user_id") + "'";
        System.out.println(sql);

        try {
            return this.execUpdate(sql);
        } catch (Exception e) {
            return false;
        }
    }

    public  boolean DeleteUser(String[] CheckBox) {
         sql = "delete from t_user where employee_id in(";

        try {
            if ((CheckBox == null) || (CheckBox.length == 0)) {
                return true;
            }

            for (int i = 0; i < CheckBox.length; i++) {
                sql = sql + "'" +
                    new String(CheckBox[i].getBytes("ISO-8859-1")) + "',";
            }

            sql = sql + "';dlsgkdsfl;gksdl;eirirepoteitoperireoprtiopet'";
            sql = sql + ")";

            //System.out.println(sql);
            this.updateNativeQuery(sql);
            return  true;
        } catch (Exception e) {
            return false;
        }
    }

//    public Vector getNodeList(Object employee_id) {
//        ArrayList sAl = new ArrayList();
//        ArrayList mAl = new ArrayList();
//        DataMap sHm = new DataMap();
//        DataMap mHm = new DataMap();
//        int sNum = 0;
//        int mNum = 0;
//
//        //查询节点一的信息 娱乐圈
//        String serviceSql = "select distinct t1.* from t_node1 as t1 ,t_node2 as t2 ,t_user_auth t3 " +
//        		"where t1.is_issue='Y' " +
//        		"and t1.node1_id=t2.node1_id " +
//        		"and t2.node2_id=t3.node2_id " +
//        		"and t3.employee_id='"+employee_id+"' "+
//        		"order by  t1.node1_id";
//        System.out.println(serviceSql);
//        //查询某角色下的某用户拥有的菜单项信息 （可以将该SQL修改后创建为视图，方便查询）
//        String moduleSql = "select t1.* from t_node2 as t1,t_user_auth t2 " +
//        		"where t1.is_issue='Y' " +
//        		"and t1.node2_id=t2.node2_id " +
//        		"and t2.employee_id='"+employee_id+"' " +
//        		"order by t1.node1_id ";
//        System.out.println(moduleSql);
//        Vector vAuth = new Vector();
//      
//
//        try {
//            sAl = this.queryALLResultSet(serviceSql);
//            mAl = this.queryALLResultSet(moduleSql);
//            sNum = sAl.size();
//            mNum = mAl.size();
//
//            for (int i = 0; i < sNum; i++) //一级菜单
//             {
//                Vector vS = new Vector();
//                sHm = (DataMap) sAl.get(i);
//
//                String s_node1_id = sHm.get("node1_id").toString().trim();
//                boolean isPutService = false;
//
//                for (int k = 0; k < mNum; k++) //二级菜单
//                 {
//                    mHm = (DataMap) mAl.get(k);
//
//                    String m_node1_id = mHm.get("node1_id").toString().trim();
//                    String m_node2_id = mHm.get("node2_id").toString().trim();
//
//                    if (m_node1_id.equals(s_node1_id)) {
//                        if (!isPutService) {
//                            vS.addElement(sHm);
//                            isPutService = true;
//                        }
//
//                        vS.addElement(mHm);
//                    }
//                }
//
//                if (vS.size() > 0) {
//                    vAuth.addElement(vS);
//                }
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//        //System.out.println("java"+vAuth);
//        return vAuth;
//    }
//    public Vector getNodeList() {
//        ArrayList sAl = new ArrayList();
//        ArrayList mAl = new ArrayList();
//        DataMap sHm = new DataMap();
//        DataMap mHm = new DataMap();
//        int sNum = 0;
//        int mNum = 0;
//
//        //查询节点一的信息
//        String serviceSql = "select * from t_mainmenus where is_use='Y' order by mainmenu_id";
//
//        //查询某角色下的某用户拥有的菜单项信息 （可以将该SQL修改后创建为视图，方便查询）
//        String moduleSql = "select * from t_submenus where is_use='Y' order by mainmenu_id";
//        Vector vAuth = new Vector();
//     
//
//        try {
//            sAl = this.queryALLResultSet(serviceSql);
//            mAl = this.queryALLResultSet(moduleSql);
//            sNum = sAl.size();
//            mNum = mAl.size();
//
//            for (int i = 0; i < sNum; i++) //一级菜单
//             {
//                Vector vS = new Vector();
//                sHm = (DataMap) sAl.get(i);
//
//                String s_node1_id = sHm.get("mainmenu_id").toString().trim();
//                boolean isPutService = false;
//
//                for (int k = 0; k < mNum; k++) //二级菜单
//                 {
//                    mHm = (DataMap) mAl.get(k);
//
//                    String m_node1_id = mHm.get("mainmenu_id").toString().trim();
//                    String m_node2_id = mHm.get("submenu_id").toString().trim();
//
//                    if (m_node1_id.equals(s_node1_id)) {
//                        if (!isPutService) {
//                            vS.addElement(sHm);
//                            isPutService = true;
//                        }
//
//                        vS.addElement(mHm);
//                    }
//                }
//
//                if (vS.size() > 0) {
//                    vAuth.addElement(vS);
//                }
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//        //System.out.println("java"+vAuth);
//        return vAuth;
//    }

    public Vector getNodeList2(String userid) {
        ArrayList sAl = new ArrayList();
        ArrayList mAl = new ArrayList();
        DataMap sHm = new DataMap();
        DataMap mHm = new DataMap();
        int sNum = 0;
        int mNum = 0;

        //查询节点一的信息
        String serviceSql = "select mainmenu_id,mainmenu_name,is_use,is_open,ctrl_date,action,orderid from t_mainmenus where is_use='Y'";
               if(userid!=null){
            	   serviceSql+=" and mainmenu_id in (select distinct n2.mainmenu_id from t_user_auth ra,t_submenus n2 where ra.submenu_id=n2.submenu_id and ra.user_id='"+userid+"')";
               }
        	   serviceSql+=" order by mainmenu_id";
        	   
        //查询某角色下的某用户拥有的菜单项信息 （可以将该SQL修改后创建为视图，方便查询）
        String moduleSql = "select submenu_id,mainmenu_id,submenu_name,ctrl_url,is_use,ctrl_date,target,action,request,orderid from t_submenus where is_use='Y'" ;
        		if(userid!=null){
        			moduleSql+=" and submenu_id in (select submenu_id  from t_user_auth where user_id='"+userid+"')" ;
        		}
        		moduleSql+=" order by mainmenu_id";
        Vector vAuth = new Vector();
       

        try {
            sAl = this.queryALLResultSet(serviceSql);
            mAl = this.queryALLResultSet(moduleSql);
            sNum = sAl.size();
            mNum = mAl.size();

            for (int i = 0; i < sNum; i++) //一级菜单
             {
                Vector vS = new Vector();
                sHm = (DataMap) sAl.get(i);

                String s_node1_id = sHm.get("mainmenu_id").toString().trim();
                boolean isPutService = false;

                for (int k = 0; k < mNum; k++) //二级菜单
                 {
                    mHm = (DataMap) mAl.get(k);

                    String m_node1_id = mHm.get("mainmenu_id").toString().trim();
                    String m_node2_id = mHm.get("submenu_id").toString().trim();

                    if (m_node1_id.equals(s_node1_id)) {
                        if (!isPutService) {
                            vS.addElement(sHm);
                            isPutService = true;
                        }

                        vS.addElement(mHm);
                    }
                }

                if (vS.size() > 0) {
                    vAuth.addElement(vS);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        //System.out.println("java"+vAuth);
        return vAuth;
    }
    
//    public ArrayList getNode1List(DataMap hm) {
//        ArrayList aNode1 = new ArrayList();
//        String sql =
//            "select n2.node1_id as node1_id from t_user_auth ra,t_node2 n2 where ra.node2_id=n2.node2_id and ra.employee_id='" +
//            hm.get("t_employee").toString() + "'";
//        DBOperate dbAction = this;
//
//        try {
//            aNode1 = dbAction.queryALLResultSet(sql);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//        return aNode1;
//    }

    public ArrayList getNode1List(DataMap hm) {
        ArrayList aNode1 = new ArrayList();
        String sql =
            "select n2.mainmenu_id from t_user_auth ra,t_submenus n2 where ra.submenu_id=n2.submenu_id and ra.user_id='" +
            hm.get("t_employee").toString() + "'";
       
        try {
            aNode1 = this.queryALLResultSet(sql);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return aNode1;
    }

    public ArrayList getNode2List(DataMap hm) {
        ArrayList aNode2 = new ArrayList();
        String sql = "select submenu_id  from t_user_auth where user_id='" +
            hm.get("t_employee").toString() + "'";
       

        try {
            aNode2 = this.queryALLResultSet(sql);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return aNode2;
    }

    public boolean RoleFuncAddDo(DataMap hm) {
       
        boolean isTrue = false;
        String nowTime = super.getNowTime("yyyy-MM-dd HH:mm:ss");
        String str = hm.get("gvalue").toString();
        String split = "|";
        StringTokenizer token = new StringTokenizer(str, split);
        String[] sqlBatch = new String[token.countTokens()];
        int i = 0;

        try {
            while (token.hasMoreTokens()) {
                String role_auth_id = KeyGenerator.getInstance()
                                                  .getNextKeyValue("t_role_auth");
                String sql =
                    "insert into t_user_auth (user_auth_id,employee_id,node2_id,ctrl_date) values('" +
                    role_auth_id + "','" + hm.get("t_employee").toString() +
                    "','" + token.nextToken() + "','" + nowTime + "')";
                sqlBatch[i] = sql;
                i++;
            }

            isTrue = this.execTransaction(sqlBatch);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return isTrue;
    }
    public boolean RoleFuncAddDo(DataMap hm,String delete_sql) {
      
        boolean isTrue = false;
        String nowTime = super.getNowTime("yyyy-MM-dd HH:mm:ss");
        String str = hm.get("gvalue").toString();
        String split = "|";
        StringTokenizer token = new StringTokenizer(str, split);
        String[] sqlBatch = new String[token.countTokens()+1];
        sqlBatch[0] = delete_sql;
        int i = 1;

        try {
            while (token.hasMoreTokens()) {
//                String role_auth_id = KeyGenerator.getInstance()
//                                                  .getNextKeyValue("t_role_auth");
                String sql =
                    "insert into t_user_auth (submenu_id,user_id) values('" +
                    token.nextToken() + "','" + hm.get("t_employee").toString() +"')";
                sqlBatch[i] = sql;
                System.out.println(sql);
                i++;
            }
            
          
            isTrue = this.execTransaction(sqlBatch);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return isTrue;
    }

    public String getDeleteT_user_auth(DataMap hm) {
        

        String sql = "delete from t_user_auth where user_id='" +
        hm.get("t_employee").toString() + "'";
        return sql;
    }
    public boolean DelOldFunc(DataMap hm) {
       
        boolean isTrue = false;

        try {
            String sql = "delete from t_user_auth where user_id='" +
                hm.get("t_employee").toString() + "'";
            this.updateNativeQuery(sql);
            isTrue = true;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return isTrue;
    }
}
